//
//  SelectAreaViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/14.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "SelectAreaViewController.h"
#import "SelectedAreaViewModel.h"
#import "AreaLeftTableCell.h"
#import "AreaRightTableCell.h"
@interface SelectAreaViewController ()
@property (nonatomic, strong) SelectedAreaViewModel *selectedAreaViewModel;

@property (weak, nonatomic) IBOutlet UITableView *mainLeftTableView;

@property (weak, nonatomic) IBOutlet UITableView *mainRightTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top_line_layout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top_line_height_layout;

@end

@implementation SelectAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftCloseBtnItem];

    self.top_line_layout.constant = 0.45;
    self.top_line_height_layout.constant = 0.45;
    
    self.selectedAreaViewModel = [[SelectedAreaViewModel alloc ] init];
    [self.selectedAreaViewModel loadData];
    
    [self.mainLeftTableView reloadData];
    [self.mainRightTableView reloadData];
    
    [self setExtraCellLineHidden:self.mainLeftTableView];
    [self setExtraCellLineHidden:self.mainRightTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.mainLeftTableView) {
        return self.selectedAreaViewModel.tableDataSource.allKeys.count;
    }else{
        return [[self.selectedAreaViewModel.tableDataSource objectForKey:self.selectedAreaViewModel.selectedLeftKey] count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.mainLeftTableView) {
        AreaLeftTableCell *leftCell = [tableView dequeueReusableCellWithIdentifier:@"AreaLeftTableCell"];
        leftCell.leftText.text = [self.selectedAreaViewModel.tableDataSource.allKeys objectAtIndex:indexPath.row];
        if ([leftCell.leftText.text isEqualToString:self.selectedAreaViewModel.selectedLeftKey]) {
            [leftCell setBackgroundColor:[UIColor whiteColor]];
        }else{
            [leftCell setBackgroundColor:GLOBLE_BACKGROUND_COLOR];
        }
        return leftCell;
    }else{
        AreaRightTableCell *rightCell = [tableView dequeueReusableCellWithIdentifier:@"AreaRightTableCell"];
        NSArray *rightList = [self.selectedAreaViewModel.tableDataSource objectForKey:self.selectedAreaViewModel.selectedLeftKey];
        rightCell.rightText.text = rightList[indexPath.row];
        return rightCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.mainLeftTableView) {
       self.selectedAreaViewModel.selectedLeftKey =   [self.selectedAreaViewModel.tableDataSource.allKeys objectAtIndex:indexPath.row];
       [self.mainLeftTableView reloadData];
       [self.mainRightTableView reloadData];
    }else{
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
